package com.guandata.cdc.model.db_type.mysql;

import lombok.Getter;

import java.util.Arrays;

/**
 * The config option scan.startup.mode specifies the startup mode for MySQL CDC consumer. The valid enumerations are:
 *
 * <pre>
 * initial (default): Performs an initial snapshot on the monitored database tables upon first startup, and continue to read the latest binlog.
 * earliest-offset: Skip snapshot phase and start reading binlog events from the earliest accessible binlog offset.
 * latest-offset: Never to perform snapshot on the monitored database tables upon first startup, just read from the end of the binlog which means only have the changes since the connector was started.
 * specific-offset: Skip snapshot phase and start reading binlog events from a specific offset. The offset could be specified with binlog filename and position, or a GTID set if GTID is enabled on server.
 * timestamp: Skip snapshot phase and start reading binlog events from a specific timestamp.
 * </pre>
 */
@Getter
public enum MysqlStartupMode {
    INITIAL("initial"),
    EARLIEST_OFFSET("earliest-offset"),
    LATEST_OFFSET("latest-offset"),
    SPECIFIC_OFFSET("specific-offset"),
    TIMESTAMP("timestamp");

    private final String code;

    MysqlStartupMode(String code) {
        this.code = code;
    }

    public static MysqlStartupMode fromCode(String mode) {
        if(mode == null) return INITIAL;
        return Arrays.stream(values())
                .filter(x -> x.code.equalsIgnoreCase(mode))
                .findFirst()
                .orElse(INITIAL);
    }
}
